﻿using Application.MainModule.OrderManagement;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Domain.MainModule.Orders;
using Domain.MainModule.Entities;
using System.Collections.Generic;

using Infraestructure.CrossCutting.IoC;

namespace Application.MainModule.Tests
{
    /// <summary>
    ///This is a test class for OrderManagementServiceTest and is intended
    ///to contain all OrderManagementServiceTest Unit Tests
    ///</summary>
    [TestClass()]
    public class OrderManagementServiceTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for AddOrder
        ///</summary>
        [TestMethod()]
        public void AddOrder_Invoke_Test()
        {
            //Arrange
            IOrderManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IOrderManagementService>();
            Guid orderId = Guid.Parse("ABCDF456-1234-5678-5678-CCCCCCEEEEEE");
            Order order = new Order()
                    {
                        OrderId = orderId,
                        ExtraInfo = "Extra Info"
                    };
            Order result = null;
            
            //Act
            target.AddOrder(order);
            
            //Assert
            result = target.FindOrderById(orderId);
            Assert.IsNotNull(result);
            Assert.AreEqual(orderId, result.OrderId);
            target.RemoveOrder(result);
        }

        /// <summary>
        ///A test for ChangeOrder
        ///</summary>
        [TestMethod()]
        public void ChangeOrder_Invoke_Test()
        {
            //Arrange
            IOrderManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IOrderManagementService>();
            Guid orderId = Guid.Parse("ABCDF456-1234-5678-5678-CCCCCCAAAAAA");
            Order order = null;
            Order result = null;
            
            //Act
            order = target.FindOrderById(orderId);
            order.ExtraInfo = "New Extra Info";
            target.ChangeOrder(order);

            //Assert
            result = target.FindOrderById(orderId);
            Assert.IsNotNull(result);
            Assert.AreEqual("New Extra Info", result.ExtraInfo);
        }

        /// <summary>
        ///A test for FindOrderById
        ///</summary>
        [TestMethod()]
        public void FindOrderById_Invoke_Test()
        {
            //Arrange
            IOrderManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IOrderManagementService>();
            Guid orderId = Guid.Parse("ABCDF456-1234-5678-5678-CCCCCCAAAAAA");
            Order order = null;

            //Act
            order = target.FindOrderById(orderId);

            //Assert
            Assert.IsNotNull(order);
            Assert.AreEqual(orderId, order.OrderId);
        }

        /// <summary>
        ///A test for FindPagedOrders pag count argument exception.
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(ArgumentException))]
        public void FindPagedOrders_InvalidPageCountThrowArgumentException_Test()
        {
            //Arrange
            IOrderManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IOrderManagementService>();

            //Act
            target.FindPagedOrders(0, -1);
        }

        /// <summary>
        ///A test for FindPagedOrders page index argument exception.
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(ArgumentException))]
        public void FindPagedOrders_InvalidPageIndexThrowArgumentException_Test()
        {
            //Arrange
            IOrderManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IOrderManagementService>();

            //Act
            target.FindPagedOrders(-1, 10);
        }

        /// <summary>
        ///A test for FindPagedOrders
        ///</summary>
        [TestMethod()]
        public void FindPagedOrders_Invoke_Test()
        {
            //Arrange
            IOrderManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IOrderManagementService>();
            List<Order> orders = null;

            //Act
            orders = target.FindPagedOrders(0,10);

            //Assert
            Assert.IsNotNull(orders);
            Assert.AreEqual(2, orders.Count);
        }

        /// <summary>
        ///RemoveOrder null order throw argument null exception.
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void RemoveOrde_NullOrderThrowArgumentNullException_Test()
        {
            //Arrange
            IOrderManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IOrderManagementService>();

            //Act
            target.RemoveOrder(null);
        }

        /// <summary>
        ///A test for RemoveOrder
        ///</summary>
        [TestMethod()]
        public void RemoveOrder_Invoke_Test()
        {
            //Arrange
            IOrderManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IOrderManagementService>();
            Guid orderId = Guid.Parse("ABCDF456-1234-5678-5678-CCCCCCEEEEEE");
            Order order = new Order()
            {
                OrderId = orderId,
                ExtraInfo = "Extra Info"
            };
            Order result = null;

            //Act
            target.AddOrder(order);
            target.RemoveOrder(order);

            //Assert
            result = target.FindOrderById(orderId);
            Assert.IsNull(result);
        }
    }
}
